import { Injectable } from "@nestjs/common";
import { DbService } from "./db.service";
import ILogger from "@/services/logger.service";
import { concatMap, of } from "rxjs";
import { UserEntity } from "./entitys/user.entity";
import IHelper from "@/services/helper.service";
import { VideoEntity } from "./entitys/video.entity";
import * as mockjs from "mockjs";

@Injectable()
export class SeedService {
  constructor(
    private readonly dbService: DbService
  ) {}
  async seed() {
    await this.createAdminstrator()
    // await this.createMockVideos()
  }

  async createMockVideos() {
    const videos = await this.dbService.videoRepository.find()
    if (videos.length <= 10) {
      let newVideos = IHelper.getArray(150).map(() => {
        let newVideo = new VideoEntity()
        newVideo.video = "/static/uploads/2024/01/23/n6psHyZAmDgyhpqBnozOZMEWAZLnBXIZ.mp4"
        newVideo.cover = "/static/uploads/2024/01/23/ufv4blnr9baD1iDAX5XH3PSDqumZGRrg.png"
        newVideo.name = `${mockjs.Random.cname()}${mockjs.Random.cword(2, 3)}`
        newVideo.uid = 1
        return newVideo
      })
      await this.dbService.videoRepository.save(newVideos)
      console.log("Create mock videos success.")
    }
  }

  async createAdminstrator() {
    try {
      const user = await this.dbService.userRepository.findOneBy({username: "admin"})
      if (!user) {
        await this.dbService.userRepository.save({
          username: "admin", password: IHelper.md5("123456")
        })
        ILogger.info(`Create admin user success. username: admin, password: 123456`)
      }
      // user.avatar = "/static/assets/06832cfc691d4612be3a91fc12b92d0d.jpg"
      // await this.dbService.userRepository.save(user)
    } catch(e) {
      ILogger.error(`Create admin user failed.`)
    }
    
    
  }
}